package Presenter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import Models.ATBeer;

public class ATBeerPresenter extends AbstractPresenter<ATBeer> {
	final String SINGLEITEM = "atBeer";
	final String MULTIITEM = "aTBeers";
	
	public ArrayList<ATBeer> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public ATBeer tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public ATBeer createItemFromJSONObject(JSONObject jsonObject){
		ATBeer item = new ATBeer();
		try{
			item.setId(jsonObject.getInt("ATBeerID"));
			item.setQty(jsonObject.getInt("ATBeerQty"));
			item.setVesselVolume(jsonObject.getDouble("servingTankVolume"));
			
			FGIViewPresenter fgiPresenter = new FGIViewPresenter();
			JSONObject jsonFGI = jsonObject.getJSONObject("atFgiID");
			item.setFgi(fgiPresenter.createItemFromJSONObject(jsonFGI));
			
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(ATBeer item){
		try{
			JSONObject json = new JSONObject();
			json.put("ATBeerID", item.getId());
			json.put("ATBeerQty", item.getQty());
			json.put("servingTankVolume", item.getVesselVolume());
			
			FGIViewPresenter fgiPresenter = new FGIViewPresenter();
			json.put("atFgiID", fgiPresenter.createJSON(item.getFgi()));
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public ATBeer getEmptyEntity(){
		ATBeer entity = new ATBeer();
		return entity;
	}
}
